package com.zhu.service;

import com.zhu.mapper.ExpendMapper;
import com.zhu.mapper.TypeMapper;
import com.zhu.mapper.UserMapper;
import com.zhu.mapper.WayMapper;
import com.zhu.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ExpendService {
    @Autowired
    ExpendMapper expendMapper;
    @Autowired
    WayMapper wayMapper;
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    UserMapper userMapper;


    public List<Transform> getAll(String username) {
        int userId= userMapper.getUserByName(username).getId(); //获取用户id
        List<Expend> expendList = expendMapper.getAll(userId); //查询此用户的所有信息
        ArrayList<Transform> list = new ArrayList<>();

        for (Expend expend : expendList) {
            Transform transform = new Transform();
            transform.setId(expend.getEid());
            transform.setTime(expend.getTime());
            transform.setTid(expend.getTypeId());
            transform.setWid(expend.getWayId());
            if(typeMapper.getById(expend.getTypeId()) != null){
                transform.setTypeName(typeMapper.getById(expend.getTypeId()).getName());//通过typeid查询，并赋值transfrom.typeName
            } else {
                transform.setTypeName("");
            }
            if(wayMapper.getById(expend.getWayId()) != null) {
                transform.setWayName(wayMapper.getById(expend.getWayId()).getName());//通过wayid查询，并赋值transfrom.wayName
            } else {
                transform.setWayName("");
            }
            transform.setMoney(expend.getExpendMoney());
            list.add(transform);
        }
        return list;
    }

    public List<Expend> getAllByAdmin() {
        return expendMapper.getAllByAdmin();
    }

    public boolean save(Expend expend) {
        int row = expendMapper.add(expend);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }


    public boolean upd(Expend expend) {
        int row = expendMapper.update(expend);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }


    public List<Transform> getByCondition(Expend ex) {

        List<Expend> expendList = expendMapper.getByCondition(ex); //查询此用户的所有信息
        ArrayList<Transform> list = new ArrayList<>();

        for (Expend expend : expendList) {
            Transform transform = new Transform();
            transform.setId(expend.getEid());
            transform.setTime(expend.getTime());
            transform.setTid(expend.getTypeId());
            transform.setWid(expend.getWayId());
            if(typeMapper.getById(expend.getTypeId()) != null){
                transform.setTypeName(typeMapper.getById(expend.getTypeId()).getName());//通过typeid查询，并赋值transfrom.typeName
            } else {
                transform.setTypeName("");
            }
            if(wayMapper.getById(expend.getWayId()) != null) {
                transform.setWayName(wayMapper.getById(expend.getWayId()).getName());//通过wayid查询，并赋值transfrom.wayName
            } else {
                transform.setWayName("");
            }
            transform.setMoney(expend.getExpendMoney());
            list.add(transform);
        }
        return list;
    }

    public List<Expend> getByAdmin(Expend expend) {
        return expendMapper.getByAdmin(expend);
    }


    public List<Map<Object,Object>> getNumAndName(TransSum tr) {
        return expendMapper.getNumAndName(tr);
    }


    public boolean del(int  id) {
        int row = expendMapper.delete(id);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }

    public TransSum summation(TransSum tr){
        return  expendMapper.getSum(tr);
    }
}

